package com.syxbbs.forum.services.Impl;

import com.syxbbs.forum.Utils.StringUtils;
import com.syxbbs.forum.common.ResultCode;
import com.syxbbs.forum.dao.MessageMapper;
import com.syxbbs.forum.dao.UserMapper;
import com.syxbbs.forum.exception.ApplicationException;
import com.syxbbs.forum.model.Message;
import com.syxbbs.forum.model.User;
import com.syxbbs.forum.services.IMessageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {

    @Resource
    MessageMapper messageMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    UserServiceImpl userService;


    @Override
    public void create(Message message) {
        if (StringUtils.isEmpty(message.getContent()) || message==null || message.getPostUserId()==null || message.getReceiveUserId()==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date=new Date();
        message.setUpdateTime(date);
        message.setCreateTime(date);
        //判断接收者是否存在
        User user = userMapper.selectByPrimaryKey(message.getReceiveUserId());
        if (user==null){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }

        int row = messageMapper.insertSelective(message);
        if (row!=1){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(ResultCode.FAILED_CREATE.toString());
        }

    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if (receiveUserId==null || receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        User user = userService.selectById(receiveUserId);
        if (user==null){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_USER_NOT_EXISTS.toString());
        }
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        //不可能为null
        if (count==null){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.toString());

        }

        return count;

    }

    @Override
    public List<Message> selectByReceiveId(Long receiveUserId) {
        if (receiveUserId==null || receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        return messageMapper.selectByReceiveId(receiveUserId);

    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if (state<0 || state>2 || id==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        Message updateMessage=new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date=new Date();
        updateMessage.setUpdateTime(date);

        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.toString());
        }


    }

    @Override
    public Message selectById(Long id) {
        if (id==null || id<0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());

        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;


    }

    //传入站内信id
    @Transactional
    @Override
    public void reply(Long repliedId, Message message) {
        if (repliedId==null||repliedId<0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        //检验站内信的状态
        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);
        if (existMessage==null||existMessage.getDeleteState()==1){

            log.warn(ResultCode.MESSAGE_USER_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.MESSAGE_USER_NOT_EXISTS.toString());


        }
        updateStateById(repliedId, (byte) 2);

        create(message);


    }


}
